let WebIM = require("../../../../../utils/webim")["default"];
let msgType = require("../../../msgtype");
let disp = require("../../../../../utils/broadcast");
let util = require("../../../../../utils/util");
Component({
  properties: {
    username: {
      type: Object,
      value: {},
    },
    chufang:{
      type: Object,
      value: {},
    },
    chatType: {
      type: String,
      value: msgType.chatType.SINGLE_CHAT,
    },
  },
  data: {
    inputMessage: "", // render input 的值
    userMessage: "", // input 的实时值
  },

  methods: {
    focus() {
      this.triggerEvent("inputFocused", null, {
        bubbles: true
      });
    },

    blur() {
      this.triggerEvent("inputBlured", null, {
        bubbles: true
      });
    },

    isGroupChat() {
      return this.data.chatType == msgType.chatType.CHAT_ROOM;
    },

    getSendToParam() {
      console.log(this.data.username.your)
      return this.isGroupChat() ? this.data.username.groupId : this.data.username.your;
    },

    // bindinput 不能打冒号！
    bindMessage(e) {
      this.setData({
        userMessage: e.detail.value,
        type: 'txt'
      });
    },

    emojiAction(emoji) {
      var str;
      var msglen = this.data.userMessage.length - 1;
      if (emoji && emoji != "[del]") {
        str = this.data.userMessage + emoji;
      } else if (emoji == "[del]") {
        let start = this.data.userMessage.lastIndexOf("[");
        let end = this.data.userMessage.lastIndexOf("]");
        let len = end - start;
        if (end != -1 && end == msglen && len >= 3 && len <= 4) {
          str = this.data.userMessage.slice(0, start);
        } else {
          str = this.data.userMessage.slice(0, msglen);
        }
      }
      this.setData({
        userMessage: str,
        inputMessage: str
      });
    },

    sendMessage() {
      let me = this;
      var TIME = util.formatTime(new Date());
      var type = me.data.type
      String.prototype.trim = function() {
        return this.replace(/(^\s*)|(\s*$)/g, '');
      }
      if (!this.data.userMessage.trim()) {
        return;
      }
      let id = WebIM.conn.getUniqueId();
      let msg = new WebIM.message(msgType.TEXT, id);
      var sate = wx.getStorageSync('sate')
      console.log(sate)
      if (type == 'txt') {
        var datatext = {
          "text": me.data.userMessage,
          "style": "right",
          'type': 'txt',
          'sate': sate,
          'time': TIME,
        }
      }
      var docroom = wx.getStorageSync('docroom')
      console.log(docroom)
      msg.set({
        msg: JSON.stringify(datatext),
        from: this.data.username.myName,
        to: docroom,
        roomType: false,
        chatType: this.data.chatType,
        success(id, serverMsgId) {
          //插入数据库

          console.log('成功了')
          disp.fire('em.chat.sendSuccess', id, me.data.userMessage);
        },
        fail(id, serverMsgId) {
          console.log('失败了')
        }
      });
      if (this.data.chatType == msgType.chatType.CHAT_ROOM) {
        msg.setGroup("groupchat");
      }
      WebIM.conn.send(msg.body);
      this.triggerEvent(
        "newTextMsg", {
          msg: msg,
          type: msgType.TEXT,
        }, {
          bubbles: true,
          composed: true
        }
      );
      //
      this.setData({
        userMessage: "",
        inputMessage: "",
      });
    },
    chufangmsd() {
      let me = this;
      var chufang = me.properties.chufang
      var TIME = util.formatTime(new Date());
      var type = 'file'
      String.prototype.trim = function () {
        return this.replace(/(^\s*)|(\s*$)/g, '');
      }
      let id = WebIM.conn.getUniqueId();
      let msg = new WebIM.message(msgType.TEXT, id);
      var sate = wx.getStorageSync('sate')
      if (type == 'file') {
        var datatext = {
          "username": chufang.username,
          'text': chufang.text,
          "title": chufang.title,
          "style": chufang.style,
          'type': chufang.type,
          'url': chufang.url,
          'sate': chufang.sate,
          'time': chufang.time,
        }
      }
      var docroom = wx.getStorageSync('docroom')
      console.log(docroom)
      msg.set({
        msg: JSON.stringify(datatext),
        from: this.data.username.myName,
        to: docroom,
        roomType: false,
        chatType: this.data.chatType,
        success(id, serverMsgId) {
          //插入数据库

          console.log('成功了')
          disp.fire('em.chat.sendSuccess', id, me.data.userMessage);
        },
        fail(id, serverMsgId) {
          console.log('失败了')
        }
      });
      if (this.data.chatType == msgType.chatType.CHAT_ROOM) {
        msg.setGroup("groupchat");
      }
      WebIM.conn.send(msg.body);
      this.triggerEvent(
        "newTextMsg", {
          msg: msg,
          type: msgType.TEXT,
        }, {
          bubbles: true,
          composed: true
        }
      );
      //
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; // 当前页
      currPage.setData({
        chufang: false
      })
      this.setData({
        userMessage: "",
        inputMessage: "",
      });
    },
  },

  // lifetimes
  created() {},
  attached() {},
  moved() {},
  detached() {},
  ready() {
    var that =this
    var sate = wx.getStorageSync('sate')
    console.log(sate)
    that.setData({
      sate: sate
    })
  },
});